package com.leather.common.toolkits;

import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class ImageKit {

    //压缩图片
    public static File compression(File file) {
        try {
            Thumbnails.of(file).scale(0.5).outputQuality(0.4f).toFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }


    public static String uploadImage(MultipartFile file,String prevfix) {
        //保存临时的图片文件
        String fileName = file.getOriginalFilename();
        String path = "F:/images";
        File targetFile = new File(path, fileName);
        try {
            file.transferTo(targetFile);
            //上传时已压缩，若图片服务器内存紧张，可取消此注释
            ImageKit.compression(targetFile);
            return QinNiuKit.getStr(prevfix, targetFile);

        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
            targetFile.delete();
        }

    }

}
